package com.ihome.android.util.exception;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.ihome.android.ActivityMgr;
import com.ihome.android.EAApp;
import com.ihome.android.common.ConfigUtil;
import com.ihome.android.util.Util;
import com.ihome.android.util.log.LogUtil;


public class CrashHandler implements UncaughtExceptionHandler {

	private static final String	TAG			= "CrashHandler";
	private static CrashHandler	INSTANCE	= new CrashHandler();
	private static Context		context;

	private CrashHandler() {}

	/** 获取CrashHandler实例 ,单例模式 */
	public static CrashHandler getInstance(Context context) {
		CrashHandler.context = context;
		return INSTANCE;
	}

	private void saveCrashLogToFile(Throwable paramThrowable) {
		StringBuffer mStringBuffer = new StringBuffer();
		Writer mWriter = new StringWriter();
		PrintWriter mPrintWriter = new PrintWriter(mWriter);
		paramThrowable.printStackTrace(mPrintWriter);
		paramThrowable.printStackTrace();
		Throwable mThrowable = paramThrowable.getCause();
		// 迭代栈队列把所有的异常信息写入writer中
		while (mThrowable != null) {
			mThrowable.printStackTrace(mPrintWriter);
			// 换行 每个个异常栈之间换行
			mPrintWriter.append("\r\n");
			mThrowable = mThrowable.getCause();
		}
		// 记得关闭
		mPrintWriter.close();
		String mResult = mWriter.toString();
		mStringBuffer.append(mResult);
		// 保存文件，设置文件名
		String mTime = Util.getTimeStamp();
		String mFileName = "CrashLog-" + mTime + ".log";
		try {
			FileOutputStream mFileOutputStream = new FileOutputStream(ConfigUtil.LOG_PATH + mFileName);
			LogUtil.d(TAG, "file path-->" + ConfigUtil.LOG_PATH + mFileName);
			mFileOutputStream.write(mStringBuffer.toString().getBytes());
			mFileOutputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		LogUtil.d(TAG, "uncaughtException ++++++++++++++");
		saveCrashLogToFile(ex);
		Writer mWriter = new StringWriter();
		PrintWriter mPrintWriter = new PrintWriter(mWriter);
		ex.printStackTrace(mPrintWriter);
		ex.printStackTrace();
		Throwable mThrowable = ex.getCause();
		// 迭代栈队列把所有的异常信息写入writer中
		while (mThrowable != null) {
			mThrowable.printStackTrace(mPrintWriter);
			// 换行 每个个异常栈之间换行
			mPrintWriter.append("\r\n");
			mThrowable = mThrowable.getCause();
		}
		// 记得关闭
		mPrintWriter.close();
		String mResult = mWriter.toString();
		LogUtil.d(TAG, "mResult" + mResult);
		new Thread() {
			@Override
			public void run() {
				Looper.prepare();
				Toast.makeText(context, "很抱歉,程序出现异常,即将退出", Toast.LENGTH_LONG).show();
				Looper.loop();
			}
		}.start();
		try {
			Thread.sleep(500);
			EAApp.getApp().getActivityMgr().finishAllActivity();
			android.os.Process.killProcess(android.os.Process.myPid());
			System.exit(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
